package com.wn.activiti.web;

import com.wn.activiti.service.ActivitiService;
import com.wn.common.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wuqingting
 * @date 2022/10/21
 */
@RestController
@RequestMapping("/activiti")
public class ActivitiController {
    @Autowired
    private ActivitiService activitiService;

    /**
     * 开启流程
     * @param entity 流程开始需要的信息
     * @return 流程实例id,将这个id存入业务表的activitiInstanceId字段,用于流程后续任务的处理和查询流程历史
     */
    @RequestMapping("/startInstance")
    public ResponseEntity startInstance(@RequestBody ActivitiStartEntity entity){
        String instanceId = null;
        try {
            instanceId = activitiService.startInstance
                    (entity.getProcessDefinitionKey(), entity.getAssignee(), entity.getUser());
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.fail;
        }

        return new ResponseEntity(instanceId);
    }

    /**
     * 执行流程节点
     * @param entity 执行流程节点需要的信息
     * @return 被执行的任务的id
     */
    @RequestMapping("/completeTask")
    public ResponseEntity completeTask(@RequestBody ActivitiCompleteEntity entity){
        String taskId = null;
        try {
            taskId = activitiService.completeTask
                    (entity.getProcessDefinitionKey(), entity.getInstanceId(), entity.getCurrUser(), entity.getNextAssignees(), entity.getNextUsers());
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.fail;
        }

        return new ResponseEntity(taskId);
    }

    /**
     * 查询流程历史信息
     * @param entity 查询流程历史信息需要的信息
     * @return 流程历史信息的集合
     */
    @RequestMapping("/historyInfo")
    public ResponseEntity<List<ActivitiInstanceHistory>> historyInfo(@RequestBody ActivitiHistoryInfoEntity entity){
        List<ActivitiInstanceHistory> historyList = null;
        try {
            historyList = activitiService.historyInfo(entity.getInstanceId());
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.fail;
        }

        return new ResponseEntity<>(historyList);
    }

    /**
     * 查询多个流程历史信息
     * @param entityList 查询流程历史信息需要的信息
     * @return 流程历史信息的集合
     */
    @RequestMapping("/historyInfoList")
    public ResponseEntity<List<List<ActivitiInstanceHistory>>> historyInfoList(@RequestBody List<ActivitiHistoryInfoEntity> entityList){
        List<List<ActivitiInstanceHistory>> allHistoryList = new ArrayList<>();
        try {
            for (ActivitiHistoryInfoEntity entity : entityList) {
                List<ActivitiInstanceHistory> historyList = activitiService.historyInfo(entity.getInstanceId());
                allHistoryList.add(historyList);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.fail;
        }

        return new ResponseEntity<>(allHistoryList);
    }
}
